<template>
	<el-main>
		<el-card shadow="never" v-loading="isLoading">
			<el-form :model="form" ref="form" :rules="rules" label-width="110" :disabled="this.action == 'view'">
				<el-header>
					<h2>基本信息</h2>
				</el-header>
				<el-main>
					<el-row>
						<el-col :span="11">
							<el-form-item label="姓名" prop="doctor_name" label-width="150">
								<el-input v-model="form.doctor_name" clearable></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="11">
							<el-form-item label="科室" prop="department_code" label-width="150">
								<el-input v-model="form.department_name" placeholder="请选择" @click="openDepartment" @clear="clearDepartment"
										  clearable></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="11">
							<el-form-item label="职称" prop="job_title" label-width="150">
								<el-select v-model="form.job_title" filterable style="width: 100%">
									<el-option v-for="item in job_title_list" :key="item.job_title" :label="item.job_title_name" :value="item.job_title"/>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="11">
							<el-form-item label="手机号" prop="mobile" label-width="150">
								<el-input v-model="form.mobile" clearable></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="11">
							<el-form-item label="擅长领域" prop="skill" label-width="150">
								<el-input v-model="form.skill" :rows="2" type="textarea" placeholder="请填写医生擅长领域" clearable></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="11">
							<el-form-item label="执业经历" prop="job_his" label-width="150">
								<el-input v-model="form.job_his" :rows="2" type="textarea" placeholder="例如：医生个人学习经历、执业经历介绍、高光时刻介绍、擅长病情介绍" clearable></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :span="11">
							<el-form-item label="执业证编号" prop="job_no" label-width="150">
								<el-input v-model="form.job_no" clearable></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</el-main>
				<el-row>
					<el-col :span="2">
						<el-form-item>
							<el-button type="primary" :loading="isSaveing" @click="submit()">保存</el-button>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</el-card>
	</el-main>
	<department-popup ref="departmentPopup" @success="departmentPopupSuccess" @closed="departmentPopupShow = false"></department-popup>
</template>
<script>
	import {verifyPhone} from "@/utils/verificate";
	import departmentPopup from './department.vue';

	export default {
		name: 'doctor_create',
		components: {departmentPopup},
		data() {
			return {
				isSaveing: false,
				form: {
					doctor_name: "",
					mobile: "",
					job_title: "",
					skill: "",
					job_his: "",
					job_no: "",
					department_code:"",
					department_name:"",
				},
				job_title_list: [],
				//验证规则
				rules: {
					doctor_name: [
						{required: true, message: '请输入姓名', trigger: 'blur'}
					],
					department_code: [
						{required: true, message: '请选择科室', trigger: 'blur'}
					],
					mobile: [
						{required: true, message: '请输入手机号', trigger: 'blur'},
						{validator: verifyPhone, trigger: "blur"}
					],
					job_title: [
						{required: true, message: '请选择职称', trigger: 'change'}
					],
					job_no: [
						{required: true, message: '请输入执业证编号', trigger: 'blur'}
					],
				},
			}
		},
		mounted() {
			//获取职称
			this.getJobTitleList()
		},
		methods: {
			getJobTitleList() {
				this.$API['admin.doctor'].getJobTitleList.get().then((res) => {
					if (res.code == 0) {
						this.job_title_list = res.data;
					}
				}).catch((msg) => {
					this.$alert(msg, "提示", {type: 'error'})
				})
			},
			onFileChange(e) {
				console.log(e);
				// this.form.file = e.target.files[0];
			},
			submit() {
				this.$refs.form.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						this.$API['admin.doctor'].store.post(this.form)
							.then((res) => {
								if (res.code == 0) {
									this.$emit('success', this.form, this.mode)
									this.$refs.form.resetFields()
									this.clearDepartment();
									this.$message.success("操作成功")
									this.$router.push({
										path: '/admin/doctor/index',
										query: {
										}
									})
								} else {
									this.$alert(res.message, "提示", {type: 'error'})
								}
							})
							.catch((msg) => {
								this.$alert(msg, "提示", {type: 'error'})
							})
							.finally(() => {
								this.isSaveing = false;
							});
					} else {
						return false;
					}
				})
			},
			downloadFailedData() {
				window.open(this.failedDownLoadUrl);
			},
			//所属科室弹窗
			openDepartment() {
				this.departmentPopupShow = true;
				this.$refs.departmentPopup.open()
			},
			//科室选择成功后
			departmentPopupSuccess(selection) {
				if (!selection){
					this.$alert('请选择科室', '提示', { type: 'error' });
					return false;
				}
				if(selection.length != 1){
					this.$alert('只能选择一个科室', '提示', { type: 'error' });
					return false;
				}
				let department_code = '';
				let department_name = '';
				selection.forEach(val => {
					department_code = val.department_code;
					department_name = val.department_name;
				});
				this.form.department_code = department_code;
				this.form.department_name = department_name;
				this.$refs.departmentPopup.close();
			},
			//清除科室
			clearDepartment(){
				this.form = {
					department_code:'',
					department_name:''
				}
			},
		}
	}
</script>

<style>
</style>
